package com.hnucm.c201901020241.discuss;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hnucm.c201901020241.R;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Discuss_information extends AppCompatActivity {
    EditText content_ET,title_discuss_ET;
    TextView word_count;
//    用户发论坛
//    存储用户发布的信息，要存储用户的账号，发布的位置，发布的title和内容
//    账号信息可用SharedPreferences存储
    String title,title_discuss,information,time,name,account,base64;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discuss_information);
        init_String();//初始化各个属性的值
        init_action();//初始化点击事件

    }

    private void init_action() {
        content_ET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                word_count.setText(content_ET.getText().length()+"字");
            }

            @Override
            public void afterTextChanged(Editable s) {
                word_count.setText(content_ET.getText().length()+"字");
            }
        });
//        返回
        findViewById(R.id.textView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        发布
        findViewById(R.id.textView2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //        获得标题
                title_discuss=title_discuss_ET.getText().toString();
//        获得文章内容
                information=content_ET.getText().toString();

                x.Ext.init(getApplication());
                RequestParams requestParams = new RequestParams("http://121.4.27.254:8090/addContent?");
                requestParams.addBodyParameter("title",title);
                requestParams.addBodyParameter("name",name);
                requestParams.addBodyParameter("time",time);
//                requestParams.addBodyParameter("base64",base64);
                requestParams.addBodyParameter("title_discuss",title_discuss);
                requestParams.addQueryStringParameter("account",account);
                requestParams.addBodyParameter("minformation",information);
                x.http().post(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if(result.equals("1")){
                            Toast.makeText(Discuss_information.this,"发送成功",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Toast.makeText(Discuss_information.this,"网络出现异常错误，请稍后",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
                finish();
            }
        });

    }

    private void init_String() {
        //        当前论坛的名字
        title=getIntent().getStringExtra("title");
        content_ET=findViewById(R.id.textView4);
        title_discuss_ET=findViewById(R.id.textView18);
        word_count=findViewById(R.id.textView1);


//        获得当前时间
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("MM-dd HH:mm:ss");
        time=sdf.format(date);
//        获取当前的用户名name，头像image
        SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE);
        name = sharedPreferences.getString("name", "test");
        base64=sharedPreferences.getString("base64","null");//头像暂时写死
        account=sharedPreferences.getString("account","null");
    }
}